package com.arpa.oms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.oms.domain.dto.OmsOutboundItemDTO;
import com.arpa.oms.domain.entity.OmsOutboundItem;
import com.arpa.oms.domain.vo.OmsOutboundItemVO;
import com.arpa.oms.mapper.OmsOutboundItemMapper;
import com.arpa.oms.service.IOmsOutboundItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * OMS出库单明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-11-07
 */
@Service
public class OmsOutboundItemServiceImpl extends ServiceImpl<OmsOutboundItemMapper, OmsOutboundItem> implements IOmsOutboundItemService {

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(OmsOutboundItem entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(OmsOutboundItem entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<OmsOutboundItem>().lambda().eq(OmsOutboundItem::getCode,entity.getCode()));
    }

    /**
     * 查询列表
     * @param omsOutboundItemDTO
     * @return
     */
    @Override
    public List<OmsOutboundItemVO> queryList(OmsOutboundItemDTO omsOutboundItemDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(omsOutboundItemDTO.getSortField())){
            omsOutboundItemDTO.setSortField(CommonUtil.camel2Underline(omsOutboundItemDTO.getSortField()));
        }
        List<OmsOutboundItemVO> omsOutboundItemVOList = baseMapper.queryList(omsOutboundItemDTO);
        return omsOutboundItemVOList;
    }

    /**
     * 查询合计,包含总数
     * @param omsOutboundItemDTO
     * @return
     */
    @Override
    public OmsOutboundItemVO queryListSum(OmsOutboundItemDTO omsOutboundItemDTO){
        OmsOutboundItemVO omsOutboundItemVO = baseMapper.queryListSum(omsOutboundItemDTO);
        return omsOutboundItemVO;
    }

}
